package com.snopy.stream;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoUrl
 * @description 我猜测是你看错了。
 * @email 77912204@qq.com
 * @date 2022/08/01 11:21
 */
@Slf4j
public class DemoUrl {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html");
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(1000);
            connection.setReadTimeout(1000);
            log.info("connecting...");
            connection.connect();
            TimeUnit.SECONDS.sleep(5);
            //BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String c  = null;
            while ((c = in.readLine())!=null){
                System.out.println(c);
            }
            in.close();
        } catch (IOException | InterruptedException e) {
            log.info("连接超时");
            throw new RuntimeException(e);
        }
    }
}
